//
//  DownloadingViewController.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 12-12-11.
//  Copyright (c) 2012年 wzyk. All rights reserved.
//

#import "DownloadingViewController.h"
#import "DownloadingCell.h"

@interface DownloadingViewController ()

@end

@implementation DownloadingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.tasksToDeleteArray = [NSMutableArray arrayWithCapacity:0];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDisplay) name:kOneDownloadFinished object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    nothingLabel.hidden = YES;
    nothingUnpaidLabel.hidden = YES;
    [self updateDisplay];
}

- (void)updateDisplay
{
    self.downloadingArray = [[DownloadManager defaultManager] getDownloading];
    [downloadingTableView reloadData];
    if (_downloadingArray.count > 0) {
        nothingLabel.hidden = YES;
        nothingUnpaidLabel.hidden = YES;
    }
    else {
        if ([[AuthorManager defaultManager] hasUserPaid]) {
            nothingLabel.hidden = NO;
            nothingUnpaidLabel.hidden = YES;
        }
        else {
            nothingLabel.hidden = YES;
            nothingUnpaidLabel.hidden = NO;
        }
    }
}

- (void)taskCompleted:(NSNotification *)notification
{
    DownloadInfo *info = notification.object;
    [_tasksToDeleteArray removeObject:[info.infoDic objectForKey:@"subitem_id"]];
    [self updateDisplay];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.downloadingArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DownloadingCell *cell = [downloadingTableView dequeueReusableCellWithIdentifier:@"DownloadingCell"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"DownloadingCell" owner:self options:nil] objectAtIndex:0];
        cell.tasksToDeleteArray = _tasksToDeleteArray;
    }
    cell.chapterIndex = indexPath.row;
    cell.downloadInfo = [self.downloadingArray objectAtIndex:indexPath.row];
    [cell updateDisplay];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [downloadingTableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

- (NSArray *)downloadingSubitemIds;
{
    NSMutableArray *array = [NSMutableArray array];
    for (DownloadInfo *info in _downloadingArray) {
        [array addObject:[info.infoDic objectForKey:@"subitem_id"]];
    }
    return array;
}

- (void)beginEdit
{
    [downloadingTableView setEditing:YES animated:YES];
    downloadingTableView.allowsMultipleSelection = YES;
}

- (void)completeEdit
{
    [downloadingTableView setEditing:NO animated:YES];
    [_tasksToDeleteArray removeAllObjects];
}

- (void)selectAll
{
    if (_tasksToDeleteArray.count == _downloadingArray.count) {
        [_tasksToDeleteArray removeAllObjects];
    }
    else {
        [_tasksToDeleteArray removeAllObjects];
        [_tasksToDeleteArray addObjectsFromArray:[self downloadingSubitemIds]];
    }
    [downloadingTableView reloadData];
}

- (void)deleteSelected
{
    for (NSString *subitemId in _tasksToDeleteArray) {
        [[DownloadManager defaultManager] cancelDownload:subitemId];
    }
    [_tasksToDeleteArray removeAllObjects];
    [self updateDisplay];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [downloadingTableView release];
    [_downloadingArray release];
    [nothingLabel release];
    [nothingUnpaidLabel release];
    [super dealloc];
}
- (void)viewDidUnload {
    [downloadingTableView release];
    downloadingTableView = nil;
    [nothingLabel release];
    nothingLabel = nil;
    [nothingUnpaidLabel release];
    nothingUnpaidLabel = nil;
    [super viewDidUnload];
}
@end
